import { getArtilesByIdAPI } from '@/api/article';

import { getDetailById } from '@/api/detail';
import { AppDispatch } from '..';
export const getArtilesByIdAction = (data: {
  channel_id?: number;
  timestamp?: number;
}) => {
  return async (dispatch: AppDispatch) => {
    const res = await getArtilesByIdAPI(data);
    console.log('article  ----->  ', res);
    dispatch({
      type: 'article/saveList',
      payload: { ...res.data, channel_id: data.channel_id },
    });
  };
};

// 3.新建一个更新的action，不去复用保存的aciton
export const updateArtilesByIdAction = ({
  channel_id = 0,
  timestamp = Date.now(),
}) => {
  return async (dispatch: AppDispatch) => {
    console.log('  ----->  ', timestamp);
    const res = await getArtilesByIdAPI({ channel_id, timestamp });
    dispatch({
      type: 'article/updateListById',
      payload: { ...res.data, channel_id },
    });
  };
};

export const getArticleByIdAction = (id: string) => {
  return async (dispatch: AppDispatch) => {
    const res = await getDetailById(id);
    console.log('res  ----->  ', res);
    dispatch({ type: 'article/saveArtDedailById', payload: res.data });
  };
};

export const clearArticleByIdAction = () => {
  return {
    type: 'article/saveArtDedailById',
    payload: {},
  };
};
